Core Java - Interview Questions and Answers for 'For loop' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'For loop' - 11 question(s) found - Order By Newest

 Q1. What are the considerations to be made in case of loops in Java ?Core Java
Ans. 1. It shouldn't result in infinite loop. Please make sure that you have a condition that will terminate the loop and that condition should be reached.

2. Make sure to use the break statement if you aspire to only look for something. Not using break will unnecessarily execute it till the end of for loop in some cases.

3. Similarly use continue to execute the loop with next iteration and bypass the rest of the code block if required.

4. Try to avoid multiple nesting of for loops. If it''s required, Make sure to use break and continue properly so as to avoid some unnecessary processing.

5. Make sure to use try catch within the loop and not outside the for loop if you expect it to continue if one of the iteration fails.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   loops   continue   break   for loop  control statements  loop statement.while loop  control statements  loop statement   architecture


 Q2. What is the difference between for and foreach loop in java ? Which one should be used in which cases ?

Core Java
Ans. 1. for loop in java is used with a counter as following

for(int counter=0;counter < 50;counter++){
System.out.println(list.get(counter));
}

for iterating and printing the contents of a collection

whereas foreach loop can be specified directly without the use of counter

for(String str:list){
System.out.println(list.get(counter));
}

2. for Each loop syntax is more clean if we have to iterate over the elements of a collection and we need not keep track of the record count

3. For is preferred when we need loops without the usage of collections or Array of objects and entirely primitives are being used

4. for loop is preferred if we need to keep track of record count and have to perform some action of the basis of that. For example - If we have to print something after every 5 records, With for each loop in such case, we will have to keep a separate counter.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loop  for loop  control statements  loop statement  foreach loop  for vs foreach loop


 Q3. What is the difference between a break statement and a continue statement?Core Java
Ans. Break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   break   continue   loop   for loop  control statements  loop statement   while loop  control statements  loop statement   break   continue   difference between   basic interview question      basic        frequent


 Q4. Will a for loop like following throw a compilation error ? for(;;);
Ans. No. It will result in infinite loop

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loop  for loop  java


 Q5. Is it a good design practice to have programming constructs like if and loops in constructor ?Design
Ans. I would avoid that. If we need to initialize member elements differently on the basis of some condition, I would prefer having overloaded constructors. I don't see a need to have a loop for initializing member elements unless the count of elements is huge and they all need to be initialized with common value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     design constructor  for loop


 Q6. What are the counter controlled repetitions in java ? Core Java
Ans. Counter controlled repetitions are the loops that are controlled with the use of counters or the loops where the number of repetitions are known in advance. for loop in java is the counter controlled repetition.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     for loop  control statements  loop statement


 Q7. What are the different types of loops in Java ?Core Java
Ans. for, while and do while

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loops  different type of loops  for loop  control statements  loop statement      basic


 Q8. What is an infinite loop ?Core Java
Ans. Infinite loop is a programming condition wherein the control goes into an infinite loop because the loop termination condition can never be met. For example -

for(int x=1;x>0;x++){
}

in this loop, with each increment the condition x > 0 will remain true till infinity.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     loop  infinite loop  for loop  control statements  loop statement      basic


 Q9. What is the difference between return and continue statement ?Core Java
Ans. return is used within a method to return control out of the method. It may be followed by a value which is returned from the method to calling method immediately preceding the point of call.

continue statement is used within a loop to start next iteration without executing the remaining statements in the loop.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     return statement  continue statement  return vs continue  for loop  control statements  loop statement


 Q10. Why shouldn't we use string concatenation extensively or in a loop ?Core Java
Ans. Because String being an immutable object creates a new object upon each concatenation cycle. If there is any such need , we should use String Builder whose objects are mutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string  string immutable  immutability  for loop  control statements  loop statement     Asked in 1 Companies


 Q11. Find unique values between two arrays using single for loop ?Core Java
 This question was recently asked at 'Net connect'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     for loop  control statements  loop statement     Asked in 1 Companies



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: